var app = new Vue({
    el: "#app",
    data: {
        // 商品名称，单价，购买数量
        list: [
            {
                id: 31,
                ProName: "草莓",
                ProPrice: 12,
                count: 1,
                isChecked: true
            },
            {
                id: 45,
                ProName: "荔枝",
                ProPrice: 15,
                count: 1,
                isChecked: true
            },
            {
                id: 99,
                ProName: "甜橙",
                ProPrice: 20,
                count: 1,
                isChecked: true
            }
        ],
        //全选
        inputChecked: true

    },

    computed: {
        totalProice: function () {

            var total = 0;

            for (var i = 0; i < this.list.length; i++) {

                var item = this.list[i];
                
                if (item.isChecked === true) {
                    total += item.ProPrice * item.count;

                }

            }
            // replace正则表示分隔符
            return total.toString().replace(/\B(?=(\d{3})+$)/g, ',');
        }
    },
    methods: {
        // count=购买数量
        handleRedece: function (index) {
            if (this.list[index].count === 1) return;
            this.list[index].count--;
        },
        handleAdd: function (index) {
            this.list[index].count++;
        },
        handleRemove: function (index) {
            this.list.splice(index, 1);
        },
        handleChange: function () {
            this.list.forEach(element => {
                element.isChecked = this.inputChecked;
            });
        },
        handleChangeOther: function () {
            var isFalse = true;
            this.list.forEach(element => {
                if (element.isChecked === false) {
                    isFalse = false;
                }
            });

            if (isFalse === false) {
                this.inputChecked = false;
            } else {
                this.inputChecked = true;
            }
        },
    }

})